#pragma once

#ifndef _CNPCHANDLER_H_
#define _CNPCHANDLER_H_

#include "CNpc.h"
#include "CZone.h"
#include "CSpawnPointGroup.h"

DWORD WINAPI ZoneServ(LPVOID lpParam);

class CZoneHandler
{

protected:
	static CZoneHandler * _instance;
private: 
	//counters for the #players in each zone
	short CZoneHandler::charsInZone[256];
	//semaphore to lock access to the counter
	CSemaphore* CZoneHandler::zoneCounterSemaphore[256];

	//static sem_t CZoneHandler::zoneCounterSemaphore[256];
	
	//semaphore to control activity of each zoneServ thread
	//static sem_t CZoneHandler::zoneActiveSemaphore[256];
	CSemaphore* CZoneHandler::zoneActiveSemaphore[256];

public:
	
	CSemaphore* CZoneHandler::mobListSemaphore[256];

	CSemaphore* CZoneHandler::spawnListSemaphore[256];

	static CZoneHandler * getInstance();
	void initZoneSemaphors(unsigned short zone);
	void increaseZoneCounter(unsigned short zone);
	void decreaseZoneCounter(unsigned short zone);
	void checkZoneActive(unsigned short zone);

	void spawnNpcs(CChar * schar);
	int  spawnMobs(unsigned char * data, unsigned short num, CChar * schar);
	// get an npc in zone by id
	CNpc * getNpc(int npcid, int zone);
    CNpc * getMob(int npcid, int zone); 
	// get an npc in zone by targid
	CNpc * getNpcByTarg(int npctargid, int zone);
	CNpc * getMobByTarg(int mobtargid, int zone);

	std::vector<struct timer> npcTimerList[256];
	// if not loaded, loads the npcs and path nodes for a zone
	int loadZoneList(unsigned short zone);
	// list of NPC's for all zones
	std::vector<CNpc*> npcZoneList[256];
	// list of MOB's for all zones
	std::vector<CNpc*> mobZoneList[256];
	// list of MOB's for all zones
	std::vector<CSpawnPointGroup*> mobSpawnZoneList[256];
	// list of pathing nodes for MOBs
	std::vector<struct pathnode> pathNodeList[256];


////////////////////////////////////////////////////////////////////////////////
	////////// NEW ZONE HANDLING 
////////////////////////////////////////////////////////////////////////////////
	
	// list to hold all CZone Objects 
	std::vector<CZone> m_zoneList;

	CZone* CZoneHandler::getZone(unsigned short zoneID);
	void CZoneHandler::loadZones();

	int CZoneHandler::changeZoneWeather(unsigned short zoneID);

	void removeMob(unsigned int mobid);

	void loadSpawnGroups(unsigned short zone);

private:
	CZoneHandler(void);
	~CZoneHandler(void);

};
#endif
